package com.ruoyi.listener.improve;

import com.ruoyi.quality.domain.bo.QualityContinuousImprovementBo;
import com.ruoyi.quality.domain.vo.QualityContinuousImprovementVo;
import com.ruoyi.quality.service.IQualityContinuousImprovementService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;

@Component
public class ApproveListener implements ExecutionListener {
    @Resource
    public IQualityContinuousImprovementService improvementService;

    private static ApproveListener approveListener;
    @PostConstruct
    public void init() {
        approveListener = this;
        approveListener.improvementService = this.improvementService;
    }

    @Override
    public void notify(DelegateExecution delegateExecution) {
        Map<String, Object> variables = delegateExecution.getVariables();
        String procId = delegateExecution.getProcessInstanceId();
        QualityContinuousImprovementVo vo = approveListener.improvementService.queryByProcessInstanceId(procId);
        QualityContinuousImprovementBo bo = new QualityContinuousImprovementBo();
        BeanUtils.copyProperties(vo,bo);
        String grade = String.valueOf(variables.get("grade"));
        if (grade.equals("1")){
            bo.setLevel(String.valueOf(variables.get("grade Level")));
        }else {
            bo.setLevel(String.valueOf(variables.get("no Grade")));
        }

        String dept = String.valueOf(variables.get("all Department"));
        if (dept!=null){
            bo.setAllDepartment(dept);
        }
        approveListener.improvementService.updateByBo(bo);

    }
}
